﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Text;
using System.Threading.Tasks;
using ZhonTai.Industry.Contracts.Communication;
using ZhonTai.Industry.Contracts.Model.Dic;
using ZhonTai.Industry.Contracts.Model.Entity;

namespace ZhonTai.Industry.Contracts.Interactive
{
    public class WatchDogMonitor : BaseMonitor
    {
        private string _addr;
        private int _type;
        private bool heartState;
        private SiemensPlc _plc;

        public WatchDogMonitor(int initialInterval = 800) : base(initialInterval)
        {
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="addr">心跳地址</param>
        /// <param name="type">写入方式：1:交互写；2：固定值写</param>
        public void Initialization(SiemensPlc plc, string addr, int type)
        {
            _plc = plc;
            _addr = addr;
            _type = type;
        }

        protected override void Execute()
        {
            try
            {
                if (_type == 1)
                {
                    _plc.Write(_addr, heartState ? (short)1 : (short)0);
                    heartState = !heartState;
                }
                else
                {
                    _plc.Write(_addr, (short)1);
                }
            }
            catch
            {
            }
        }
    }
}